<template>
    <div class="update-gender">
        <van-picker
           title="性别"
           show-toolbar
           :default-index="defaultIndex"
           :columns="columns"
           @confirm="onConfirm"
           @cancel="$emit('close')"
        />
    </div>
  </template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateGender',
  components: {},
  props: {
    value: {
      type: Number,
      required: true

    }
  },
  data () {
    return {
      columns: ['男', '女'],
      defaultIndex: this.value

    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onConfirm (value, index) {
      if (index === this.value) {
        this.$toast('性别未修改')
        return
      }
      try {
        this.$toast.loading({
          duration: 0, // 持续时间，0表示持续展示不停止
          forbidClick: true, // 是否禁止背景点击
          message: '用户性别修改中...' // 提示消息
        })
        await updateUserProfile({
          gender: index
        })
        this.$emit('input', index)
        this.$emit('close')
        this.$toast.success('性别更新成功')
      } catch (err) {
        window.console.log(err)
        this.$toast.fail('性别更新失败')
      }
    }
  }
}
</script>

  <style scoped lang="less">
 .field-wrap {
  padding: 20px;
  background-color: #d8d8d8;
}
  </style>
